<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/19
 * Time: 18:33
 */

namespace app\admin\model;


class CardIncordec extends DataBase
{
    protected $name = 'card_incordec';
    protected $autoWriteTimestamp = true;
    private $giftBagType = [
        1=>'幸运礼袋',
        2=>'百宝礼袋',
        3=>'七彩礼袋',
        4=>'黄金宝袋',
        5=>'至尊礼包'
    ];

    public function getDataList()
    {
        $page_config = array(
            'page' => input('offset',1),
            'list_rows' => input('limit',10),
        );
        $where = [];
        if(!empty(input('start_time')) && !empty(input('end_time')) && input('time_key')=='cre'){
            $start = strtotime(input('start_time')); $end =strtotime(input('end_time'));
            $where[] =['a.create_time','>=',$start];
            $where[] = ['a.create_time','<',$end];
        }
        if(!empty(input('start_time')) && !empty(input('end_time')) && input('time_key')=='upd'){
            $start = strtotime(input('start_time')); $end =strtotime(input('end_time'));
            $where[] =['a.update_time','>=',$start];
            $where[] = ['a.update_time','<',$end];
        }
        if(!empty(input('key')) && !empty(input('value'))){
            $key = input('key'); $value = input('value');
            if($key=='u_id' || $key == 'nickname') $where[] = ['c.'.$key,'like','%'.$value.'%'];
            if($key == 'incordec_status'){
                switch($value){
                    case '增加': $where[] = ['a'.$key,'=',1];break;
                    case '消耗': $where[] = ['a.'.$key,'=',2];break;
                }
            }
            if($key == 'incordec_type'){
                switch($value){
                    case '系统赠送': $where[] = ['a.'.$key,'=',1];break;
                    case '签到赠送': $where[] = ['a.'.$key,'=',2];break;
                    case '他人赠送': $where[] = ['a.'.$key,'=',3];break;
                    case '邀请赠送': $where[] = ['a.'.$key,'=',4];break;
                    case '送给他人': $where[] = ['a.'.$key,'=',5];break;
                    case '合成座驾': $where[] = ['a.'.$key,'=',6];break;
                    case '合成礼物': $where[] = ['a.'.$key,'=',7];break;
                    case '合成礼袋': $where[] = ['a.'.$key,'=',8];break;
                }
            }
            if($key == 'auditing'){
                switch($value){
                    case '已审核': $where[] = ['a.'.$key,'=',2];break;
                    case '未审核': $where[] = ['a.'.$key,'=',3];break;
                }
            }
            if($key == 'status'){
                switch($value){
                    case '进行中': $where[] = ['a.'.$key,'=',1];break;
                    case '成功': $where[] = ['a.'.$key,'=',2];break;
                    case '失败': $where[] = ['a.'.$key,'=',3];break;
                }
            }
        }
        $order = input('sort', 'a.id') . ' ' . input('order', 'desc');

        $data = $this
            ->alias('a')
            ->field('a.*,b.name,c.u_id,c.nickname')
            ->join('card b','a.card_id = b.id','left')
            ->join('users c','a.user_id = c.id','left')
            ->where($where)
            ->order($order)
            ->paginate($page_config);

        foreach($data as $key => $value){
            switch($value['incordec_type']){
                case 1: //系统赠送
                    $data[$key]['event'] = '系统赠送';
                    break;
                case 2: //签到赠送
                    $data[$key]['event'] = '签到赠送';
                    break;
                case 3: //他人赠送
                    $user = User::where('id','=',$value['incorde_id'])->field('nickname')->find();
                    $nickname = preg_replace_callback('/@E(.{6}==)/', function ($r) {
                        return base64_decode($r[1]);
                    }, $user['nickname']);
                    $data[$key]['event'] = '由'.$nickname.'赠送';
                    break;
                case 4: //邀请赠送
                    $user = User::where('id','=',$value['incorde_id'])->field('nickname')->find();
                    $nickname = preg_replace_callback('/@E(.{6}==)/', function ($r) {
                        return base64_decode($r[1]);
                    }, $user['nickname']);
                    $data[$key]['event'] = '邀请'.$nickname.'赠送';
                    break;
                case 5: //送给他人
                    $user = User::where('id','=',$value['incorde_id'])->field('nickname')->find();
                    $nickname = preg_replace_callback('/@E(.{6}==)/', function ($r) {
                        return base64_decode($r[1]);
                    }, $user['nickname']);
                    $data[$key]['event'] = '赠送给'.$nickname;
                    break;
                case 6: //合成座驾
                    $vehicle = Vehicle::where('vehicle_id ','=',$value['incorde_id'])->field('vehicle_name')->find();
                    $data[$key]['event'] = '合成座驾--'.$vehicle['vehicle_name'];
                    break;
                case 7: //合成礼物
                    $gift =GiftUrl::where('id','=',$value['incorde_id'])->field('gift_name')->find();
                    $data[$key]['event'] = '合成礼物--'.$gift['gift_name'];
                    break;
                case 8: //合成礼袋
                    if($value['auditing'] == 3){
                        $incorde_id = $value['incorde_id'];
                        $data[$key]['event'] = $this->giftBagType[$incorde_id];
                    }
                    if($value['auditing'] == 2){
                        $gift = GiftUrl::where('id','=',$value['incorde_id'])->field('gift_name')->find();
                        $data[$key]['event'] = '礼袋礼物--'.$gift['gift_name'];
                    }
                    break;
            }
        }

        return $data;
    }
}